import android
import time
from datetime import datetime
from contacts_tools import ContactTools

droid = android.Android()

class SMSTools(android.Android):
    def __init__(self):
        super(SMSTools, self).__init__()
        self.messages =  self.smsGetMessages('unreadOnly', 'inbox', None)
        self.sorted_messages = {}
        
        self.ct = ContactTools()
        
        
    def print_messages(self):
        for message in self.messages[1]:
            print str(message), '\n---------------------\n'
            
            
    def _sort_messages(self):
        for message in self.messages[1]:
            address = str(message['address'])[2:]
            message_body = message['body']
            message_date = message['date']
            if self.sorted_messages == {} or address not in self.sorted_messages.keys():
                self.sorted_messages[address] = [{'body':message_body, 'date':message_date}]
            elif address in self.sorted_messages.keys():                
                self.sorted_messages[address].append({'body':message_body, 'date':message_date})
            else:
                print 'there was another outcome!!!'
        return self.sorted_messages
    
    def _print_sorted_messages(self):
        sorted_messages = self._sort_messages()
        for key in sorted_messages.keys():
            print '\n', key, '\n-------------------------->'
            for message in sorted_messages[key]:
                try:
                    print message['body']
                except:
                    pass
                    
            
    def _fetch_associated_contact(self, contact_id):
        contacts = self.ct.contacts
        self.print_messages()
        raw_input('>>>>')
        for contact in contacts:
            print contact, '\n'
    

        
        
if __name__ == '__main__':
    smstools = SMSTools()
    #smstools._print_sorted_messages()
    smstools._fetch_associated_contact(2)
    
    
